define(function(require, exports, module) {
	var product = function() {
		require('zTree');
		var setting = {};
		$.get(HOST + '/manage/category/cateList', function(json) {
			console.log(json);
		}, 'json');

		$(document).ready(function() {
			$.fn.zTree.init($("#category_tree"), setting, zNodes);
		});

	}

	var category = function() {
		require('zTree');
		var setting = {
			keep:{
				parent:true,
			},
			edit:{
				drag: {
					isCopy: true,
					isMove: true
				},
				enable:true ,
				showRemoveBtn : true,
				showRenameBtn : true,
				removeTitle : "删除类目",
				renameTitle : "重命名"
			},
			data : {
				simpleData : {
					enable : true,
					idKey : 'id',
					pIdKey : 'parent',
				}
			},
			callback:{
				beforeRemove:function(treeId,treeNode){
					return confirm('确认删除['+treeNode.name+']节点');
				},
				onRename:function(event,treeId,treeNode){
					$.post(HOST+'/manage/category/update',{'name':treeNode.name,'id':treeNode.id});
				}
			}
		};
		$.get(HOST + '/manage/category/cateList', function(json) {
			if (json.status == 0) {
				var data = json.data;
				$.fn.zTree.init($("#category_tree"), setting, data);
			} else {
				alert(json.msg);
			}
		}, 'json');
		
		
		
		//bind saveCateList
		$('#saveCateList').click(function(){
			var cateTree = $.fn.zTree.getZTreeObj('category_tree');
			var nodes = cateTree.transformToArray(cateTree.getNodes());
			console.log(nodes);
		});
		
		var newCount = 1;
		$('#saveAddCate').click(function(){
			var cateTree = $.fn.zTree.getZTreeObj('category_tree');
			var newCateName = $('#newCategoryName').val();
			var treeNode = $('#myModal').data('treeNode');
			var pid = treeNode?treeNode.id:0;
			var pNode = treeNode?treeNode:null;
			console.log(treeNode);
			newCount++;
			cateTree.addNodes(pNode, {id:(100 + newCount), pId:pid, isParent:false, name:newCateName});
			$('#myModal').modal('hide');
		});
		
		$('#myModal').on('show',function(json){
			var treeNode = $(this).data('treeNode');
			$('#newCategoryName').val('');
			var showName = '';
			if(treeNode){
				showName = treeNode.name+'子类';
			}else{
				showName = '顶级分类';
			}
			$('#addParentName').html(showName);
		});
		
		//add category
		$('.addNode').click(function(){
			var cateTree = $.fn.zTree.getZTreeObj('category_tree'),
			nodes = cateTree.getSelectedNodes();
			treeNode = nodes[0];
			$('#myModal').data('treeNode',treeNode).modal('show');
		});
		
	}
	
	
	var createProduct = function(category_id){
		require('zTree');
		var setting = {
				check: {
					enable: true,
					chkStyle: "radio",
					radioType: "all"
				},
				data : {
					simpleData : {
						enable : true,
						idKey : 'id',
						pIdKey : 'parent',
					}
				},
				callback:{
					onCheck:function(event,treeId,treeNode){
						 $('#Product_category_id').val(treeNode.id);
					}
				}
			};
			$.get(HOST + '/manage/category/cateList', function(json) {
				if (json.status == 0) {
					var data = json.data;
					var cateTree = $.fn.zTree.init($("#product_category"), setting, data);
					
					var treeNode = cateTree.getNodeByParam('id',category_id,null);
					cateTree.expandNode(treeNode,true,true);
					cateTree.checkNode(treeNode,true,true);
				} else {
					alert(json.msg);
				}
			}, 'json');
			//-->
	}

	module.exports = {
		'product' : product,
		'category' : category,
		'createProduct' : createProduct 
		
	};
});

